package com.dxq.wan;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import com.dxq.basic_ui_library.TabView;
import com.dxq.wan.fragment.BaseFragment;
import com.dxq.wan.fragment.Tab1Fragment;
import com.dxq.wan.fragment.Tab2Fragment;
import com.dxq.wan.fragment.Tab3Fragment;
import com.dxq.wan.fragment.Tab4Fragment;
import com.dxq.wan.fragment.Tab5Fragment;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    private FrameLayout frameLayout;
    private TabView tabView1, tabView2, tabView3, tabView4, tabView5;
    private List<TabView> tabButtonList = new ArrayList<>();

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    public BaseFragment currentFragment, tab1Fragment, tab2Fragment, tab3Fragment, tab4Fragment, tab5Fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        //初始化Fragment
        initFragment();
        initTabEvent();
    }

    private void initView() {
        frameLayout = (FrameLayout) this.findViewById(R.id.frameLayout);
        tabView1 = (TabView) this.findViewById(R.id.tabView1);
        tabView2 = (TabView) this.findViewById(R.id.tabView2);
        tabView3 = (TabView) this.findViewById(R.id.tabView3);
        tabView4 = (TabView) this.findViewById(R.id.tabView4);
        tabView5 = (TabView) this.findViewById(R.id.tabView5);
        tabView1.setIconAndText(R.mipmap.ic_launcher, R.mipmap.ic_launcher, "1");
        tabView2.setIconAndText(R.mipmap.ic_launcher, R.mipmap.ic_launcher, "2");
        tabView3.setIconAndText(R.mipmap.ic_launcher, R.mipmap.ic_launcher, "3");
        tabView4.setIconAndText(R.mipmap.ic_launcher, R.mipmap.ic_launcher, "4");
        tabView5.setIconAndText(R.mipmap.ic_launcher, R.mipmap.ic_launcher, "5");
        tabButtonList.add(tabView1);
        tabButtonList.add(tabView2);
        tabButtonList.add(tabView3);
        tabButtonList.add(tabView4);
        tabButtonList.add(tabView5);
    }

    private void initFragment() {
        //得到FragmentManger
        fragmentManager = this.getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        //默认加载第一个fragment
        tab1Fragment = Tab1Fragment.newInstance("1");
        currentFragment = tab1Fragment;
        fragmentTransaction.replace(R.id.frameLayout, currentFragment, "1");
        fragmentTransaction.commit();
        //设置当前显示tab
        setCurrentTab(0);
    }

    //tab点击事件
    private void initTabEvent() {
        tabView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == tab1Fragment) {
                    tab1Fragment = new Tab1Fragment().newInstance("1");
                }
                showFragment(tab1Fragment, "1");
                setCurrentTab(0);
            }
        });
        tabView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == tab2Fragment) {
                    tab2Fragment = new Tab2Fragment().newInstance("2");
                }
                showFragment(tab2Fragment, "2");
                setCurrentTab(1);
            }
        });
        tabView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == tab3Fragment) {
                    tab3Fragment = new Tab3Fragment().newInstance("3");
                }
                showFragment(tab3Fragment, "3");
                setCurrentTab(2);
            }
        });
        tabView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == tab4Fragment) {
                    tab4Fragment = new Tab4Fragment().newInstance("4");
                }
                showFragment(tab4Fragment, "4");
                setCurrentTab(3);
            }
        });
        tabView5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == tab5Fragment) {
                    tab5Fragment = new Tab5Fragment().newInstance("5");
                }
                showFragment(tab5Fragment, "5");
                setCurrentTab(4);
            }
        });
    }

    private void setCurrentTab(int pos) {
        for (int i = 0; i < tabButtonList.size(); i++) {
            TabView tabView = tabButtonList.get(i);
            if (i == pos) {
                tabView.setProgress(1);
            } else {
                tabView.setProgress(0);
            }
        }
    }

    private void showFragment(BaseFragment fg, String tag) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (!fg.isAdded()) {
            fragmentTransaction.hide(currentFragment).add(R.id.frameLayout, fg, tag);
        } else {
            fragmentTransaction.hide(currentFragment).show(fg);
        }
        //全局变量，记录当前显示的fragment
        currentFragment = fg;
        fragmentTransaction.commit();
    }


}
